package basicCompiler.visitor;

import java.util.Stack;
import basicCompiler.Node;
import basicCompiler.AbstractOperand;
import basicCompiler.AbstractOperator;



// Use single dispach
public class TreeVisitor implements AbstractTreeVisitor{
	
	@Override
	public void visit(AbstractOperand op, Stack<Node> results,
			Stack<AbstractOperator> operators) {
		results.push(op);
		
	}

	@Override
	public void visit(AbstractOperator op, Stack<Node> results,
			Stack<AbstractOperator> operators) {
		
		boolean done = false;
		while ( !( done)) {
			if ( operators.isEmpty()) {
				operators.push(op);
				done = true;
			} else {
				if ( op.compareTo(operators.peek()) >= 0) {
					operators.push(op);
					done = true;
				}
				else {
					Node right = results.pop();
					Node left = results.pop();
					Node o = operators.pop();
					o.setChildren(left, right);
					results.push(o);
				}
			}
		}
	}
}
